<?php
class Route_Content
{
    /**
     * Build friendly URL
     * @param array $linkArr 0 => <module>, 1 => controller, 2 => action, 3...n param/value
     * @param array $params param => value
     * 
     * @return string friendly URL
     */
    public function build($linkArr, $params = array()) 
    {
    	
    	$result = implode('/', $linkArr);
    	
    	/**
    	 * Link is detail
    	 */
    	if ('detail' == @$linkArr[2]) {
    		/**
	    	 * Structure: /<category-alias>/<alias>.html
	    	 */
    		require_once 'modules/content/models/ContentCategory.php';
    		$objCat = new Models_ContentCategory();
    		$content = @Nine_Registry::getContentByGid($linkArr[4]);
    		$cat = @reset($objCat->getByColumns(array('content_category_gid=?' => $content['content_category_gid']))->toArray());
    		
    		if ('Static' == $cat['name']) {
    			/**
    			 * Category static
    			 */
	    		if (null != @$content->alias) {
	    			$result = urlencode(@$content->alias);
	    		}
    		}
    		else {
    			if (null != @$content->alias) {
	    			$result = $cat['alias'] . '/' .urlencode(@$content->alias);
	    		}
    		}
    	}
    	if ('index' == @$linkArr[2]) {
    		/**
	    	 * Structure: catagories/<alias>.html
	    	 */
    		$result = "categories/";
	    	if (null != @$params['alias']) {
	    		$result .= urlencode(@$params['alias']);
	    	}else {
	    		require_once 'modules/content/models/ContentCategory.php';
    			$objCat = new Models_ContentCategory();
	    		$cat = @reset($objCat->getByColumns(array('content_category_gid=?' => @$linkArr[4]))->toArray());
	    		if (null != @$cat['alias']) {
	    			$result .= urlencode(@$cat['alias']);
	    		}
	    	}
    		 
    	}
//    	if ('popup' == @$linkArr[2]) {
//    		/**
//	    	 * Structure: pages/<id>/<alias>.html
//	    	 */
//    		$result = "content/popup/{$linkArr[4]}/";
//	    	if (null != @$params['alias']) {
//	    		$result .= urlencode(@$params['alias']);
//	    	}else {
//	    		$content = @Nine_Registry::getContentByGid($linkArr[4]);
//	    		if (null != @$content->alias) {
//	    			$result .= urlencode(@$content->alias);
//	    		}
//	    	}
//    		 
//    	}
    	return $result;
    	
    }
    /**
     * Parse friendly URL
     */
    public function parse()
    {
         $router = Nine_Controller_Front::getInstance()->getRouter();
         $route  = new Zend_Controller_Router_Route_Regex(
                                                 '(?!^contact-us)(.*).html',
                                                 array(
                                                    'module'     => 'content',
                                                    'controller' => 'index',
                                                    'action'     => 'detail'
                                                 ),
                                                 array(1 =>'alias')
                                            );
         $router->addRoute('content', $route); 
         
         $route1  = new Zend_Controller_Router_Route_Regex(
                                                 '.*/(.*).html',
                                                 array(
                                                    'module'     => 'content',
                                                    'controller' => 'index',
                                                    'action'     => 'detail'
                                                 ),
                                                 array(1 =>'alias')
                                            );
         $router->addRoute('content1', $route1); 

         $route2  = new Zend_Controller_Router_Route_Regex(
                                                 'categories/(.*).html',
                                                 array(
                                                    'module'     => 'content',
                                                    'controller' => 'index',
                                                    'action'     => 'index'
                                                 ),
                                                 array(1 =>'alias')
                                            );
         $router->addRoute('content2', $route2); 
         
//         $route3  = new Zend_Controller_Router_Route_Regex(
//                                                 'content/popup/([0-9]+)/(.*).html',
//                                                 array(
//                                                    'module'     => 'content',
//                                                    'controller' => 'index',
//                                                    'action'     => 'popup'
//                                                 ),
//                                                 array(1 =>'cid')
//                                            );
//         $router->addRoute('content3', $route3); 
         
    }
}